// store/user.ts
import { defineStore } from "pinia";
import { ref } from "vue";
import storage from "@/utils/storage";
import { UserInfo } from "@/utils/storage";

export const useUserStore = defineStore("user", () => {
  const user = ref<UserInfo | null>(storage.getUser());

  const isLoggedIn = computed(() => !!user.value);

  // 关键修改：明确接收 UserInfo 类型的参数
  const login = (userInfo: any) => {
    // 直接赋值（确保响应式）
    user.value = userInfo;
    // 存入 sessionStorage
    storage.setUser(userInfo);
  };

  const logout = () => {
    user.value = null;
    storage.removeUser();
  };

  return { user, isLoggedIn, login, logout };
});
